
from pandas import Series, DataFrame
from numpy import nan as NA
import numpy as np
#
obj = Series([4, 5, 6, -7])
print(obj)
# 0    4
# 1    5
# 2    6
# 3   -7
# dtype: int64
print(obj.dtype)    # int64
print(obj.index)    # RangeIndex(start=0, stop=4, step=1)
print(obj.values)   # [ 4  5  6 -7]


obj2 = Series([4, 5, 6, -7], index=['d', 'c', 'b', 'a'])
print(obj2)
obj2['c'] = 88
print(obj2)
# d     4
# c    88
# b     6
# a    -7
# dtype: int64
print('a' in obj2)  # True
print('e' in obj2)  # False


d = {'beijing':110, 'shanghai':121, 'guangzhou':120, 'shenzhen':755}
obj3 = Series(d)
obj3.index = ['bj', 'sh', 'gz', 'sz']
print(obj3)
# bj    110
# sh    121
# gz    120
# sz    755
# dtype: int64


obj4 = {'city':['shanghai', 'beijing', 'guangzhou', 'shenzhen'],
        'year':[2017,       2016,       2015,       2018]}
frame = DataFrame(obj4)
print(frame)
# 先按year再按city排序
frame = DataFrame(obj4, columns=['year', 'city'])
print(frame)
#    year       city
# 0  2017   shanghai
# 1  2016    beijing
# 2  2015  guangzhou
# 3  2018   shenzhen

# 提取某一列数据
print(frame.year)
# 0    2017
# 1    2016
# 2    2015
# 3    2018
# Name: year, dtype: int64
print(frame['city'])
# 0     shanghai
# 1      beijing
# 2    guangzhou
# 3     shenzhen
# Name: city, dtype: object

# 添加一个新的列，赋值100
frame['new'] = 100
# 添加一个新的列，如果是北京赋值True
frame['capital'] = frame.city == 'beijing'
print(frame)
#    year       city  new  capital
# 0  2017   shanghai  100    False
# 1  2016    beijing  100     True
# 2  2015  guangzhou  100    False
# 3  2018   shenzhen  100    False

# 删除一列，axis=0表示行，axis=1表示列
frame = frame.drop(['new'], axis=1)
# 删除一列，指定某一列
frame = frame.drop(columns=['capital'])
print(frame)
#    year       city
# 0  2017   shanghai
# 1  2016    beijing
# 2  2015  guangzhou
# 3  2018   shenzhen
# 删除一行
print(frame.drop([2]))
#    year      city
# 0  2017  shanghai
# 1  2016   beijing
# 3  2018  shenzhen
# 删除一行
print(frame.drop(index=[0, 1]))
#    year       city
# 2  2015  guangzhou
# 3  2018   shenzhen

# 行列转置
print(frame.T)
#              0        1          2         3
# year      2017     2016       2015      2018
# city  shanghai  beijing  guangzhou  shenzhen

obj5 = Series([1, 2, 3, 5], index=['d', 'c', 'b', 'a'])
# reindex修改索引的顺序, e如果没有用fill_value=0指定默认值，则是NaN
obj6 = obj5.reindex(['a', 'b', 'c', 'd', 'e'], fill_value=0)
print(obj6)

obj7 = Series(['red', 'green', 'blue'], index=[0, 2, 4])
# ffill表示1，3，5索引将被上一行数据填充，bfill将被下一行数据填充
print(obj7.reindex(range(6), method='bfill'))
# 0      red
# 1    green
# 2    green
# 3     blue
# 4     blue
# 5      NaN
# dtype: object

obj8 = Series([1, NA, 3])
# 删除一维的一行
print(obj8.dropna())
# 0    1.0
# 2    3.0
# dtype: float64

obj9 = DataFrame([[1., 2, 3], [1, NA, NA], [NA, NA, NA]])
# 只有一行中有一个NA，就会删掉整行
print(obj9.dropna())
#      0    1    2
# 0  1.0  2.0  3.0
# 只有当一行都是NA时，就会删掉整行
print(obj9.dropna(how='all'))
#      0    1    2
# 0  1.0  2.0  3.0
# 1  1.0  NaN  NaN

# 整列都是NA时，就会删掉整列
obj9[3] = NA
print(obj9)
#      0    1    2   3
# 0  1.0  2.0  3.0 NaN
# 1  1.0  NaN  NaN NaN
# 2  NaN  NaN  NaN NaN
print(obj9.dropna(axis=1, how='all'))
#      0    1    2
# 0  1.0  2.0  3.0
# 1  1.0  NaN  NaN
# 2  NaN  NaN  NaN

# 将所有的NaN填充为0，如果不加inplace=True，仅修改副本
obj9.fillna(0, inplace=True)
print(obj9)
#      0    1    2    3
# 0  1.0  2.0  3.0  0.0
# 1  1.0  0.0  0.0  0.0
# 2  0.0  0.0  0.0  0.0

obj10 = Series(np.random.randn(10),
               index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'],
                      [1, 2, 3, 1, 3, 5, 1, 6, 1, 7]])
print(obj10)
# a  1   -0.328792
#    2   -0.890057
#    3    1.475100
# b  1   -0.525922
#    3    1.574938
#    5    1.876487
# c  1    0.558727
#    6    1.250995
# d  1    0.199186
#    7    0.036812
# dtype: float64
print(obj10['c':'d'])
# c  1    0.558727
#    6    1.250995
# d  1    0.199186
#    7    0.036812
# dtype: float64
print(obj10['c'][1])    # 0.5587265709528391
print(obj10['c'][6])    # 1.250995272383842

# 将一维数据转为二维数据
print(obj10.unstack())
#           1         2         3         5         6         7
# a -0.328792 -0.890057  1.475100       NaN       NaN       NaN
# b -0.525922       NaN  1.574938  1.876487       NaN       NaN
# c  0.558727       NaN       NaN       NaN  1.250995       NaN
# d  0.199186       NaN       NaN       NaN       NaN  0.036812
# 将二维数据转为一维数据
#print(obj10.unstack().stack())